package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;



    /*
     * 新增菜品和对应的口味
     * @author Lukang
     * @date 2024/11/24 下午11:58
     * @param dishDTO
     */
    //这个方法要操作两张表，dish表和flavor口味表，所以要用事务，保持方法的四性
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {

        //获取disDTO的数据并复制到dish对象中
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        //将dish插入到dish表中
        dishMapper.insert(dish);

        /*获取dish_id。由于这里要先插入dish才能获取dish_id
        所以在dish的insert的sql语句上添加了seGeneratedKeys="true" keyProperty="id"
        这代表插入数据后就可以拿到dish_id而且id的代号为keyProperty
        */
        Long dishId = dish.getId();

        //将flavor插入到flavor表中
        //获取flavors
        //flavors中没有dish_id的数据，所以在遍历时把dish_id加进去
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            //向口味表中插入口味数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /*
     * 菜品分页查询
     * @author Lukang
     * @date 2024/12/2 下午11:25
     * @param dishPageQueryDTO
     * @return com.sky.result.PageResult
     */
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page =  dishMapper.pageQuery(dishPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /*
     * 菜品批量删除
     * @author Lukang
     * @date 2024/12/2 下午11:48
     * @param ids
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //判断菜品是否可以删除---在启售状态的无法删除
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断菜品是否能否彻底被删除--是否关联了套餐
       List<Long> setmealIds =  setmealDishMapper.getSetmealIdsByDishIds(ids);
        if (setmealIds != null && setmealIds.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //排除前面两种情况则可以删除菜品
        for (Long id : ids) {
            dishMapper.deleteById(id);
            dishFlavorMapper.deleteByDishId(id);
        }
    }
}
